package mytool;

import android.os.Handler;
import android.os.Message;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;


/**
 * Created by zerdoor_pc on 2015/10/12.
 */
public class HttpGetAndPost {
private static HttpUtils http = new HttpUtils();
  private  static String s="";
    private static int failcount = 0;
    public static void httpNoParams(String url, HttpRequest.HttpMethod postorget, final Handler hander, final int i, final int columns) {
        http.configCurrentHttpCacheExpiry(0);
        http.send(postorget, url, new RequestCallBack<String>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
//                Log.i("dc", "total===" + total + "        current===" + current + "          isUploading=" + isUploading);
//                        textView.setText(current + "/" + total);
            }

            @Override
            public void onSuccess(final ResponseInfo<String> responseInfo) {

//                Log.i("refend", "true");
                failcount = 0;
//                Log.i("dc", "responseInfo===" + responseInfo.RESULT);
//                        responseInfo.RESULT
//                        textView.setText(responseInfo.RESULT);
//                s = responseInfo.RESULT+"";

                        Message msg=new Message();
                        msg.what=i;
                        msg.arg1=columns;
                        msg.obj=responseInfo.result;
                        hander.sendMessage(msg);
            }


            @Override
            public void onFailure(HttpException error, String msg) {
                failcount++;
                FailedCallback();
                try {
                    Message msgs=new Message();
                    msgs.what=99;
                    msgs.obj=msg;
                    hander.sendMessage(msgs);
                }
                catch (Exception e)
                {

                }
            }
        });

    }

    public static void HTTP(String url, int postorget, final Handler hander, final int i, final int columns) {
//        JsonObjectRequest stringRequest = new JsonObjectRequest(postorget, url,
//                new Response.Listener<JSONObject>() {
//                    @Override
//                    public void onResponse(final JSONObject response) {
//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                Message msg=new Message();
//                                msg.what=i;
//                                msg.arg1=columns;
//                                msg.obj=(Object) response;
//
//                                hander.sendMessage(msg);
//                            }
//                        }).start();
//                    }
//                }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                Log.e("TAG", error.getMessage(), error);
//            }
//        });
    }

    public static void  FailedCallback()
    {

    }

    public static void httpNoParams(String url, HttpRequest.HttpMethod postorget, final Handler hander, final int i) {


        http.configCurrentHttpCacheExpiry(0);
        http.send(postorget, url, new RequestCallBack<String>() {
            @Override
            public void onStart() {
            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
//                Log.i("dc", "total===" + total + "        current===" + current + "          isUploading=" + isUploading);
//                        textView.setText(current + "/" + total);
            }

            @Override
            public void onSuccess(final ResponseInfo<String> responseInfo) {
                failcount = 0;
//                Log.i("dc", "responseInfo===" + responseInfo.RESULT);
//                        responseInfo.RESULT
//                        textView.setText(responseInfo.RESULT);
//                s = responseInfo.RESULT+"";
                //Log.i("","baseConfig=="+responseInfo.result);
                        Message msg=new Message();
                        msg.what=i;
                        msg.obj=responseInfo.result;
                        hander.sendMessage(msg);


            }


            @Override
            public void onFailure(HttpException error, String msg) {
                failcount++;
                FailedCallback();
                try {
                    Message msgs=new Message();
                    msgs.what=99;
                    msgs.obj=msg;
                    hander.sendMessage(msgs);
                }
                catch (Exception e)
                {

                }

            }
        });

    }


    public static void httpHasParams (String url, RequestParams params, HttpRequest.HttpMethod postorget, final Handler hander,final int i) {

        http.configCurrentHttpCacheExpiry(0);
        http.send(postorget, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {

            }
            @Override
            public void onLoading(long total, long current, boolean isUploading) {

            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                failcount = 0;
                Message msg=new Message();
                msg.what=i;
                msg.obj=responseInfo.result;
                hander.sendMessage(msg);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                failcount++;
                FailedCallback();
                try {
                    Message msgs=new Message();
                    msgs.what=99;
                    msgs.obj=msg;
                    hander.sendMessage(msgs);
                }
                catch (Exception e)
                {

                }
            }
        });


    }


}
